package util;
import java.io.*;


@SuppressWarnings("unchecked")
public class FileClassLoader extends ClassLoader {
	
	public FileClassLoader() {
		super(FileClassLoader.class.getClassLoader());
	}
	
	public Class loadClass(String className) throws ClassNotFoundException {
        return findClass(className);
   }
    
    public Class findClass(String className) throws ClassNotFoundException {
    	byte[] classBytes;
    	Class result=null;
    	
    	try {
    		return findSystemClass(className);
    	} catch (Exception e) {
    	}
    	
    	try {
    		classBytes = loadClassData(className);
    		result = defineClass(className, classBytes, 0, classBytes.length, null);
    	} catch (Exception e) {
    		throw new ClassNotFoundException();
    	}
    	
		return result;
    	
    }
    
    public byte[] loadClassData(String className) throws IOException {
		File f = new File(className.replace('.', File.pathSeparatorChar)+".class");
		byte[] buffer = new byte[(int)f.length()];
		
		DataInputStream in = new DataInputStream(new FileInputStream(f));
		
		in.readFully(buffer);
    	
    	return buffer;
    	
    }

}
